package baiduClub.schedule;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class FileBrownser extends Activity 
{
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == Menu.FIRST) 
		{
			SharedPreferences settings = getSharedPreferences("baiduClub.schedule", 0);
			SharedPreferences.Editor editor = settings.edit();
			
			System.out.println(">-----------------<" + xmlDir );
			editor.putString("xmlDir", xmlDir);
			editor.commit();
			
			Intent intent = new Intent();
			intent.setClass(this, MainActivity.class);
			startActivity(intent);
			finish();
			setResult(RESULT_OK);
		}
		else if (item.getItemId() == Menu.FIRST + 1) 
		{
			finish();
		}
		
		
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// TODO Auto-generated method stub
		menu.add(0, Menu.FIRST, 0, getString(R.string.confirmButtonString));
		menu.add(0, Menu.FIRST + 1, 0, getString(R.string.cancelButtonString));
		return super.onCreateOptionsMenu(menu);
	}

	private ListView mainListView = null;
	private List<Map<String, Object>> list = null;
	private String xmlDir = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setTitle("选择文件");
		mainListView = new ListView(this);
		setContentView(mainListView);
		
		list_init(getSDPath());
		
	}
	
	public String getSDPath()
	{
    	File sdDir = null;
    	boolean sdCardExist = Environment.getExternalStorageState()
    	.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    	if (sdCardExist)
    	{
    	sdDir = Environment.getExternalStorageDirectory();//获取跟目录
    	}
    	return sdDir.toString();

    }
	
	public void list_init (String path)
	{
		File file = new File(path);
		File[] files = file.listFiles();
		list = new ArrayList<Map<String,Object>>();
		HashMap<String, Object> item = new HashMap<String, Object>();
		
		if (path.equals(getSDPath()))
		{
			item.put("icon", R.drawable.home);
			item.put("name", "SD卡根目录");
			item.put("path", getSDPath());
			list.add(item);
		}else
		{
			item.put("icon", R.drawable.back);
			item.put("name", "返回上一级");
			item.put("path", file.getParent());
			list.add(item);
		}
		
		if( files != null)
			for (int i = 0; i < files.length; i++) 
			{
				item = new HashMap<String, Object>();
				
				//判断是否是文件夹并选取对应的图标
				if (files[i].isDirectory()) 
				{
					item.put("icon", R.drawable.folder);
				}
				else
				{
					item.put("icon", R.drawable.fileicon);
				}
				
				item.put("name", files[i].getName());
				item.put("path", files[i].getAbsolutePath());
				list.add(item);
			}
		
		FileAdapterThis adapterThis = new FileAdapterThis(this, list);
		mainListView.setAdapter(adapterThis);
		
		mainListView.setOnItemClickListener(new OnItemClickListener(){  
            public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
                if(arg2>0 && (Integer)(list.get(arg2).get("icon"))==R.drawable.fileicon){  
                	
                	xmlDir = list.get(arg2).get("path") + "";
                	
                }else{  
                    //打开下一级文件目录列表  
                    list_init((String)(list.get(arg2).get("path")));  
                }  
            }  
        });  
        this.setTitle(path);  
		
	}
	
	private class FileAdapterThis extends BaseAdapter
	{
		Context context = null;
		List<Map<String, Object>> list = null;

		public FileAdapterThis(Context context, List<Map<String, Object>> list)
		{
			this.context = context;
			this.list = list;
		}
		
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			LinearLayout returnView=new LinearLayout(context);    
            returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams     
            //图标  
            ImageView iv=new ImageView(context);    
            LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);      
            lp_iv.rightMargin=10;  
            iv.setLayoutParams(lp_iv);   
            iv.setScaleType(ScaleType.CENTER_INSIDE);    
            iv.setImageResource((Integer)((list.get(position)).get("icon")));    
            returnView.addView(iv);  
            //文件名   
            TextView name=new TextView(context);    
            LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);   
            name.setLayoutParams(lp_tv);    
            name.setTextSize(name.getTextSize()+10);    
            name.setText((String)(list.get(position).get("name")));  
            returnView.addView(name);  
            //  
            return returnView;    
		}
		
	}
	
	
	
}
